package org.groupg.practice.data;

import org.groupg.practice.data._1000wData.MemoryMappedDataStore;
import org.groupg.practice.data._500wData.HighVolumeDataStore;

import java.util.ArrayList;
import java.util.List;

public class OptimizationDemo {
    public static void main(String[] args) throws Exception {
        // 测试5kw数据量
//        int dataSize = 50_000_000;

        int dataSize = 41_297_762;
        System.out.println("=== 传统方式 vs FastUtil优化 ===");

        // 传统方式 - List of Objects
        long startTime = System.currentTimeMillis();
        List<DataRecord> traditionalList = new ArrayList<>(dataSize);
        for (int i = 0; i < dataSize; i++) {
            traditionalList.add(new DataRecord(
                i,
                System.currentTimeMillis() + i,
                Math.random() * 1000,
                "label_" + (i % 1000)
            ));
        }
        long traditionalTime = System.currentTimeMillis() - startTime;

        // FastUtil优化方式
        startTime = System.currentTimeMillis();
        HighVolumeDataStore optimizedStore = new HighVolumeDataStore(dataSize);
        for (int i = 0; i < dataSize; i++) {
            optimizedStore.addRecord(
                i,
                System.currentTimeMillis() + i,
                Math.random() * 1000,
                "label_" + (i % 1000)
            );
        }
        long optimizedTime = System.currentTimeMillis() - startTime;

        System.out.printf("传统方式耗时: %,d ms%n", traditionalTime);
        System.out.printf("优化方式耗时: %,d ms%n", optimizedTime);
        System.out.printf("性能提升: %.1f%%%n",
            (traditionalTime - optimizedTime) * 100.0 / traditionalTime);

        // 内存使用对比
        optimizedStore.printMemoryStats();

        // 测试查询性能
        testQueryPerformance(optimizedStore);

        // 测试内存映射方式（适合超大数据集）
        testMemoryMappedStorage(dataSize);
    }

    private static void testQueryPerformance(HighVolumeDataStore store) {
        System.out.println("\n=== 查询性能测试 ===");

        // 范围查询测试
        long startTime = System.currentTimeMillis();
        final int[] count = {0};

        store.processInRange(0, Long.MAX_VALUE, (id, ts, value, label) -> {
            if (value > 500.0) {
                count[0]++;
            }
        });

        long queryTime = System.currentTimeMillis() - startTime;
        System.out.printf("范围查询耗时: %,d ms, 结果数: %,d%n", queryTime, count[0]);

        // 标签查询测试
        startTime = System.currentTimeMillis();
        List<DataRecord> results = store.findByLabel("label_500");
        long labelQueryTime = System.currentTimeMillis() - startTime;
        System.out.printf("标签查询耗时: %,d ms, 结果数: %,d%n",
            labelQueryTime, results.size());
    }

    private static void testMemoryMappedStorage(int dataSize) throws Exception {
        System.out.println("\n=== 内存映射存储测试 ===");

        MemoryMappedDataStore mmapStore = new MemoryMappedDataStore("data.bin", dataSize);

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < Math.min(dataSize, 100000); i++) { // 测试10万条
            mmapStore.writeRecord(i, i, System.currentTimeMillis(),
                Math.random() * 1000, "mmap_label_" + i);
        }
        long writeTime = System.currentTimeMillis() - startTime;

        startTime = System.currentTimeMillis();
        DataRecord record = mmapStore.readRecord(50000);
        long readTime = System.currentTimeMillis() - startTime;

        System.out.printf("内存映射写入耗时: %,d ms%n", writeTime);
        System.out.printf("内存映射读取耗时: %,d ms%n", readTime);
        System.out.printf("示例记录: ID=%d, Value=%.2f%n", record.getId(), record.getValue());
    }
}